<template>
  <a-drawer width="50%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="卡名称" prop="name" >
        <a-input v-model="form.name" placeholder="请输入卡名称" />
      </a-form-model-item>
      <a-form-model-item label="卡类型" prop="cardType" >
        <a-radio-group v-model="form.cardType" button-style="solid" @change="changeType">
          <a-radio-button v-for="(d, index) in cardTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <template v-if="form.cardType != 3 && form.cardType != -99 ">
        <a-form-model-item label="数量" prop="count" v-if="form.cardType != 2">
          <a-input v-model="form.count" placeholder="请输入数量" />
        </a-form-model-item>
        <a-form-model-item label="有效期" prop="time" v-if="form.cardType != 4">
          <a-input v-model="form.time" placeholder="请输入有效期" />
          <a-radio-group v-model="form.timeType" button-style="solid">
            <a-radio-button v-for="(d, index) in timeTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
          </a-radio-group>
        </a-form-model-item>
      </template>
      <a-form-model-item label="起止日期" v-if="form.cardType == 4">
        <a-range-picker @change="timeChange" :format="YYYY-MM-DD" :valueFormat="YYYY-MM-DD" v-model="dates">
          <a-icon slot="suffixIcon" type="smile" />
        </a-range-picker>
      </a-form-model-item>
      <a-form-model-item label="状态" prop="status" >
        <a-radio-group v-model="form.status" button-style="solid">
          <a-radio-button v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      
      <a-form-model-item label="卡片封面" prop="avatar" >
        <file-upload v-model="form.avatar" type="image" ></file-upload>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getCard, addCard, updateCard } from '@/api/manager/card'
import moment from 'moment'

export default {
  name: 'CreateForm',
  props: {
    cardTypeOptions: {
      type: Array,
      required: true
    },
    timeTypeOptions: {
      type: Array,
      required: true
    },
    statusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      dates:[],
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,
        name: null,
        cardType: null,
        timeType: '1',
        status: null,
        count: null,
        avatar: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        start: null,
        end:null,
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],cardType: [
          { required: true, message: '卡类型不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    timeChange(value,dateString){
      this.form.start = dateString[0];
      this.form.end = dateString[1];
    },
    changeType(){
      
    },
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,
        name: null,
        cardType: null,
        timeType: '1',
        status: null,
        count: null,
        avatar: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        time:null,
        start: null,
        end:null,
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getCard(id).then(response => {
        this.form = response.data
        this.form.cardType = response.data.cardType.toString()
        this.form.timeType = response.data.timeType.toString()
        this.form.status = response.data.status.toString()
        this.open = true
        if(this.form.start){
          this.dates = [moment(this.form.start),moment(this.form.end)]
        }
        
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateCard(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addCard(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
